package com.jiuli_shoppingpro.controller;

import com.jiuli_shoppingpro.pojo.Goods.Goods;
import com.jiuli_shoppingpro.pojo.Goods.mvc.GoodsDetailInfo;
import com.jiuli_shoppingpro.pojo.Goods.mvc.GoodsInfo;
import com.jiuli_shoppingpro.pojo.GoodsCategory.mvc.KGoodsCategory;
import com.jiuli_shoppingpro.pojo.consumer.Address;
import com.jiuli_shoppingpro.pojo.consumer.myOrder.mvc.OrderInfo;
import com.jiuli_shoppingpro.service.ConsumerService;
import com.jiuli_shoppingpro.utils.Result;
import com.jiuli_shoppingpro.utils.jwt.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/consumer")
@CrossOrigin        /*解决跨域问题*/
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @GetMapping("/register")
    //注册
    public Result register(@RequestParam String username, @RequestParam String password) {
        String msg = consumerService.register(username, password);
        if ("注册成功".equals(msg)) {
            return Result.success(msg);
        }
        return Result.error(msg);
    }

    @GetMapping("/login")
    //登录
    public Result login(@RequestParam(name = "username") String mobile, @RequestParam(name = "password") String password) {
        String msg = consumerService.login(mobile, password);
        if ("该手机号未注册，请先注册".equals(msg) || "您输入的密码不对，请重新输入".equals(msg)) {
            return Result.error(msg);
        }
//        设置token返回给前端
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("consumerId", msg);
        String token = JwtUtils.generateJwt(claims);
        user user = new user();
        user.setUserId(mobile);
        user.setPassword(password);
        return Result.success(user);
    }

    @GetMapping("/selectAllGoodsCategory")
    //查询所有的商品分类情况
    public Result selectAllGoodsCategory() throws Exception {
        KGoodsCategory kGoodsCategory = consumerService.selectAllGoodsCategory();
        return Result.success(kGoodsCategory);
    }


    @GetMapping("/selectGoodsInfoByGoodsCategoryId")
    //查询某个分类的所有商品信息（根据商品分类id来查询）
    public Result selectGoodsInfoByGoodsCategoryId(@RequestParam     int categoryId) {
        List<GoodsInfo> goodsInfos = consumerService.selectGoodsInfoByGoodsCategoryId(categoryId);
        return Result.success(goodsInfos);
    }

    @GetMapping("/selectGoodsDetail")
    //查看商品详情页（根据商家和商品id来查）
    public Result selectGoodsDetail(@RequestParam int storeId, @RequestParam  int goodsId) {
        GoodsDetailInfo goodsDetailInfo = consumerService.selectGoodsDetail(storeId, goodsId);
        return Result.success(goodsDetailInfo);
    }


    @GetMapping("/selectGoodsByGoodsName")
    //根据商品名称来模糊查询商品基础信息
    public Result selectGoodsByGoodsName(String goodsName) {
        List<Goods> goods = consumerService.selectGoodsByGoodsName(goodsName);
        return Result.success(goods);
    }

    /* 对订单的操作 */

    @GetMapping("/addOrder")
    //加入订单
    public Result addOrder(@RequestParam String consumerId, @RequestParam   int storeId,@RequestParam     int addressId, @RequestParam   int goodsId,@RequestParam   int skuId, @RequestParam   int goodsNum,@RequestParam   int payStatus) {
        String msg = consumerService.addOrder(consumerId, storeId, addressId, goodsId, skuId, goodsNum, payStatus);
        if ("余额不足，付款失败".equals(msg)) {
            return Result.error(msg);
        }
        return Result.success(msg);
    }
    //获取商品信息
    @GetMapping("/getvb")
    public Result getvb(){
        String msg= consumerService.getvb();
        return Result.success(msg);
    }

    @GetMapping("/selectMyOrders")
    //查看所有的订单信息（根据status_text进行分类）
    public Result selectMyOrders(@RequestParam("userId") String consumerId) {
        List<OrderInfo> orderInfos = consumerService.selectMyOrders(consumerId);
        return Result.success(orderInfos);
    }

    @GetMapping("/delOrderByOne")
    //删除某订单信息
    public Result delOrderByOne(String consumerId, int orderId) {
        String msg = consumerService.delOrderByOne(consumerId, orderId);
        if ("该订单不可删除,删除失败".equals(msg)) {
            return Result.error(msg);
        }
        return Result.success(msg);
    }

    @GetMapping("/requestRefund")
    //申请退款（向商家去申请：如果是待发货状态的话，申请退款无需通过商家，直接退款，反之则否）
    public Result requestRefund(String consumerId, int orderId) {
        String msg = consumerService.requestRefund(consumerId, orderId);
        if ("商家余额不足，请联系商家退款".equals(msg)) {
            return Result.error(msg);
        }
        return Result.success(msg);
    }

    @GetMapping("/payOrderMoney")
    //订单付款
    public Result payOrderMoney(String consumerId, int orderId) {
        String msg = consumerService.payOrderMoney(consumerId, orderId);
        if (msg.contains("失败")) {
            return Result.error(msg);
        }
        return Result.success(msg);
    }

    /*对地址的操作*/

    @GetMapping("/setDefaultAddress")
    //设置默认收货地址
    public Result setDefaultAddress(String consumerId, int addressId) {

        String msg = consumerService.setDefaultAddress(consumerId, addressId);

        return Result.success(msg);
    }

    //查看默认收获地址
    public Result selectDefaultAddress(String consumerId) {

        Address address = consumerService.selectDefaultAddress(consumerId);

        if (address == null) {
            return Result.error("未设置默认收货地址");
        }
        return Result.success(address);
    }

    //查看消费者的所有收货地址信息

    @GetMapping("/insertAddress")
    //添加收货地址
    public Result insertAddress(String consumerId, String name, String phone, String province,
                                String city, String region, String detail) {

        String msg = consumerService.insertAddress(consumerId, name, phone, province, city, region, detail);

        if ("添加收货地址成功".equals(msg)) {
            return Result.success(msg);
        }

        return Result.error(msg);
    }

    @GetMapping("/updateAddress")
    //更新收获地址
    public Result updateAddress(Address address) {

        String msg = consumerService.updateAddress(address);

        return Result.success(msg);

    }

    @GetMapping("/delAddressByAddressId")
    //删除收货地址
    public Result delAddressByAddressId(String consumerId, int addressId) {

        String msg = consumerService.delAddressByAddressId(consumerId, addressId);

        return Result.success(msg);
    }

}















